
/* Generated by Interface Builder */
#import <appkit/publicWraps.h>
#import <appkit/Application.h>
#import <appkit/Listener.h>
#import <appkit/Speaker.h>
#import <appkit/Window.h>
#import <string.h>
#import <appkit/nextstd.h>
#import <cthreads.h>
#import "UtilityObject.h"

@implementation UtilityObject

-appDidInit:sender
{
    unsigned int wn;
    id speaker = [NXApp appSpeaker];
	
    NXConvertWinNumToGlobal([[NXApp appIcon] windowNum], &wn);
    [speaker setSendPort:NXPortFromName(NX_WORKSPACEREQUEST, NULL)];
    [speaker registerWindow:wn toPort:[[NXApp appListener] listenPort]];	
    return self;
}

- printFiles:anObject
/* Send the selected files to the printer. We start from the end of the list.
 * Probably should start from the beginning...
 */
{
    char *fileName, *thePath, *ptr;
    char command[1024];
    BOOL loopEnd = FALSE;
    
    while (loopEnd == FALSE)
       {
       fileName = iconPathList;
       if (ptr = rindex(iconPathList, '/'))
          {
	  *ptr = '\0';
	  fileName = ptr + 1;
	  if (ptr = rindex(iconPathList, '\t'))
	     {
	     *ptr = '\0';
	     thePath = ptr + 1;
	     }
	  else
	     {
	     thePath = iconPathList;
	     loopEnd = TRUE;
	     }
	  chdir(thePath);
	  }
       if ( (ptr = rindex(fileName, '.')) &&
          (memcmp(ptr + 1, "h", 1) == 0 ||
          memcmp(ptr + 1, "m", 1) == 0) )
	  {
          sprintf(command, "vgrind %s", fileName);
          system(command);
	  }
       else
          NXLogError("DockPrint encountered invalid file extension.");
       }

    if (iconPathList)
       {
       NX_FREE(iconPathList);
       iconPathList = NULL;
       }
    return anObject;
}

static any_t printThread(id anObject)
{   
    [anObject printFiles:anObject];
    return anObject;
}   

- (int)iconEntered:(int)windowNum at:(double)x :(double)y
    iconWindow:(int)iconWindowNum 
    iconX:(double)iconX iconY:(double)iconY
    iconWidth:(double)iconWidth 
    iconHeight:(double)iconHeight
    pathList:(char *)pathList
/* Sometimes this method is not invoked when it should be. The app appears to
 * be out of touch, clicking it will wake it up.
 */
{
    if (iconPathList)
       NX_FREE(iconPathList);
    iconPathList = NXCopyStringBuffer(pathList);
        
    return 0;
}

- (int)iconReleasedAt:(double)x :(double)y ok:(int *)flag
{    
    cthread_detach(cthread_fork((cthread_fn_t)printThread, (any_t)self));
    [NXApp hide:self];
    *flag = 1;
    return 0;
}

- (int)iconExitedAt:(double)x :(double)y
{
    if (iconPathList)
       {
       NX_FREE(iconPathList);
       iconPathList = NULL;
       }
    return 0;  
}

@end
